<template>
  <div style="min-width: 300px;width: 100%;text-align: center;">
    <div>
      <el-button :type="buttonType" :disabled="!device.online" v-on:click="handleClick('ON')" class="button-fixed">
        <i style="color:#ffffff" class="el-icon-switch-button"></i>{{$t('device.TurnOn')}}
      </el-button>
      <el-button :type="buttonType" :disabled="!device.online" v-on:click="handleClick('OFF')" class="button-fixed">
        <i style="color:#ffffff" class="el-icon-switch-button"></i>{{$t('device.TurnOff')}}
      </el-button>
    </div>
    <div>
      <el-form inline>
        <el-form-item :label="$t('device.Voltage')">{{device.online ? voltage:'--'}} V</el-form-item>
        <el-form-item :label="$t('device.Current')">{{device.online ? current:'--'}} A</el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
  import BaseController from './BaseController'
  export default {
    name: 'ScreenController',
    mixins: [BaseController],
    data() {
      return {
        current: 0,
        voltage: 0
      }
    },
    mounted() {
      //console.log(this.device)
    },
    methods: {
      setItemDevice(device) {
        const code = device.code
        const json = device.map
        if (!json) {
          return
        }
        if (code.includes("ROW_RELAY") || code.indexOf('SCREEN_RELAY_') >= 0) {
          if (json && json.ENERGY) {
            const data = json.ENERGY
            this.current = data.Current
            this.voltage = data.Voltage
          }
          return
        }
        if (code.indexOf('BleGate_') === 0 && json !== undefined) {
          this.current = json.Current / 1000
          this.voltage = json.Voltage
          return
        }
        console.warn('screen code is not start with SCREEN_RELAY/ROW_RELAY/BleGate_ : ', device)
      },
      handleClick(operate) {
        const code = this.device.code
        const action = this.$t('ON' === operate ? 'On' : 'Off')
        if (code.indexOf('SCREEN_RELAY_') >= 0) {
          const command = 'backlog'
          this.doCommand(code, command, content, action)
          return
        }
        if (code.indexOf('BleGate_') >= 0) {
          const data = {
            PackageType: 0,
            DeviceControl: {
              DevMac: code.split('@').pop(),
              Message: {}
            }
          }
          if (operate === 'ON') {
            data.DeviceControl.Message = {
              "IrSend": {
                "Mode": 0,
                "Order": 1,
                "Temp": 0
              }
            }
            this.deviceCommand("control", JSON.stringify(data), action)
            data.DeviceControl.Message = {
              "CodeSend": 1
            }
            this.deviceCommand("control", JSON.stringify(data))
          } else if (operate === 'OFF') {
            data.DeviceControl.Message = {
              "IrSend": {
                "Mode": 0,
                "Order": 0,
                "Temp": 0
              }
            }
            this.deviceCommand("control", JSON.stringify(data), action)
            data.DeviceControl.Message = {
              "CodeSend": 2
            }
            this.deviceCommand("control", JSON.stringify(data))
          }
        }
      }
    }
  }

</script>
